package com.atguigu.lease.web.admin.service.impl;

import com.atguigu.lease.common.ecception.LeaseException;
import com.atguigu.lease.common.result.ResultCodeEnum;
import com.atguigu.lease.model.entity.*;
import com.atguigu.lease.model.enums.ItemType;
import com.atguigu.lease.web.admin.mapper.FacilityInfoMapper;
import com.atguigu.lease.web.admin.mapper.FeeValueMapper;
import com.atguigu.lease.web.admin.mapper.LabelInfoMapper;
import com.atguigu.lease.web.admin.service.*;
import com.atguigu.lease.web.admin.vo.apartment.ApartmentDetailVo;
import com.atguigu.lease.web.admin.vo.apartment.ApartmentItemVo;
import com.atguigu.lease.web.admin.vo.apartment.ApartmentQueryVo;
import com.atguigu.lease.web.admin.vo.apartment.ApartmentSubmitVo;
import com.atguigu.lease.web.admin.vo.fee.FeeValueVo;
import com.atguigu.lease.web.admin.vo.graph.GraphVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.atguigu.lease.web.admin.mapper.ApartmentInfoMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;

/**
* @author asus
* @description 针对表【apartment_info(公寓信息表)】的数据库操作Service实现
* @createDate 2024-08-16 15:37:00
*/
@Service
public class ApartmentInfoServiceImpl extends ServiceImpl<ApartmentInfoMapper, ApartmentInfo>
    implements ApartmentInfoService{
@Autowired
ApartmentInfoMapper apartmentInfoMapper;
@Autowired
    ApartmentFacilityService apartmentFacilityService;
    @Autowired
    ApartmentLabelService apartmentLabelService;
    @Autowired
    ApartmentFeeValueService apartmentFeeValueService;
    @Autowired
    GraphInfoService graphInfoService;
    @Autowired
    ProvinceInfoService provinceInfoService;
    @Autowired
    CityInfoService cityInfoService;
    @Autowired
    DistrictInfoService districtInfoService;
    @Autowired
    LabelInfoMapper labelInfoMapper;
    @Autowired
    FacilityInfoMapper facilityInfoMapper;
    @Autowired
    FeeValueMapper feeValueMapper;
    @Autowired
    RoomInfoService roomInfoService;

    @Override
    public IPage<ApartmentItemVo> pageItem(IPage<ApartmentInfo> page, ApartmentQueryVo queryVo) {

        return apartmentInfoMapper.pageItem(page,queryVo);

    }

    @Override
    public void saveOrUpdateInfo(ApartmentSubmitVo apartmentSubmitVo) {
        ProvinceInfo provinceInfo = provinceInfoService.getById(apartmentSubmitVo.getProvinceId());
        String name1 = provinceInfo.getName();
        CityInfo cityInfo = cityInfoService.getById(apartmentSubmitVo.getCityId());
        String name2 = cityInfo.getName();
        DistrictInfo districtInfo = districtInfoService.getById(apartmentSubmitVo.getDistrictId());
        String name3 = districtInfo.getName();
        apartmentSubmitVo.setProvinceName(name1);
        apartmentSubmitVo.setCityName(name2);
        apartmentSubmitVo.setDistrictName(name3);
        //实现公寓基本信息的添加或修改
        saveOrUpdate(apartmentSubmitVo);
        //获取公寓id
        Long apartmentId = apartmentSubmitVo.getId();
        if (apartmentId!=null){
            apartmentFacilityService.remove(new LambdaQueryWrapper<ApartmentFacility>().eq(ApartmentFacility::getApartmentId,apartmentId));
            apartmentLabelService.remove(new LambdaQueryWrapper<ApartmentLabel>().eq(ApartmentLabel::getApartmentId,apartmentId));
            apartmentFeeValueService.remove(new LambdaQueryWrapper<ApartmentFeeValue>().eq(ApartmentFeeValue::getApartmentId,apartmentId));
            graphInfoService.remove(new LambdaQueryWrapper<GraphInfo>().eq(GraphInfo::getItemId,apartmentId).eq(GraphInfo::getItemType, ItemType.APARTMENT));

            List<Long> facilityInfoIds = apartmentSubmitVo.getFacilityInfoIds();
            if (!CollectionUtils.isEmpty(facilityInfoIds)){
                //保存公寓id和对应配套id
                List<ApartmentFacility> list = new ArrayList<>();
                for (Long facilityInfoId : facilityInfoIds) {
                    ApartmentFacility apartmentFacility = new ApartmentFacility();
                    apartmentFacility.setApartmentId(apartmentId);
                    apartmentFacility.setFacilityId(facilityInfoId);
                    list.add(apartmentFacility);
                }
                //实现批量添加
                apartmentFacilityService.saveBatch(list);
            }
            //2.插入图片列表
            List<GraphVo> graphVoList = apartmentSubmitVo.getGraphVoList();
            if (!CollectionUtils.isEmpty(graphVoList)) {
                ArrayList<GraphInfo> graphInfoList = new ArrayList<>();
                for (GraphVo graphVo : graphVoList) {
                    GraphInfo graphInfo = new GraphInfo();
                    graphInfo.setItemType(ItemType.APARTMENT);
                    graphInfo.setItemId(apartmentId);
                    String[] split = graphVo.getUrl().split("/");
                    String fileName = split[split.length - 1];

                    graphInfo.setName(fileName);
                    graphInfo.setUrl(graphVo.getUrl());
                    graphInfoList.add(graphInfo);
                }
                graphInfoService.saveBatch(graphInfoList);
            }


            //3.添加公寓和标签的关系
            List<Long> labelIds = apartmentSubmitVo.getLabelIds();
            if (!CollectionUtils.isEmpty(labelIds)) {
                List<ApartmentLabel> list = new ArrayList<>();
                for (Long labelId : labelIds) {
                    ApartmentLabel apartmentLabel = new ApartmentLabel();
                    apartmentLabel.setApartmentId(apartmentId);
                    apartmentLabel.setLabelId(labelId);
                    list.add(apartmentLabel);
                }
                apartmentLabelService.saveBatch(list);

            }
//4.插入杂费列表
            List<Long> feeValueIds = apartmentSubmitVo.getFeeValueIds();
            if (!CollectionUtils.isEmpty(feeValueIds)) {
                ArrayList<ApartmentFeeValue> apartmentFeeValueList = new ArrayList<>();
                for (Long feeValueId : feeValueIds) {
                    ApartmentFeeValue apartmentFeeValue = new ApartmentFeeValue();
                    apartmentFeeValue.setApartmentId(apartmentSubmitVo.getId());
                    apartmentFeeValue.setFeeValueId(feeValueId);
                    apartmentFeeValueList.add(apartmentFeeValue);
                }
                apartmentFeeValueService.saveBatch(apartmentFeeValueList);
            }

        }
    }

    @Override
    public ApartmentDetailVo getgetDetailById(Long id) {
        //查询公寓基本信息
        ApartmentInfo apartmentInfo = apartmentInfoMapper.selectById(id);
        //根据公寓id查询公寓的图片
       List<GraphVo> graphVoList =   graphInfoService.listGraphInfoByApartId(id);
       //根据公寓的id查询公寓的标签
       List<LabelInfo> labelInfoList = labelInfoMapper.listLabelInfoByApartId(id);
       //根据公寓的id查询公寓的配套
       List<FacilityInfo> facilityInfoList =  facilityInfoMapper.listFacilityInfoByApartId(id);
        //根据公寓的id查询公寓的杂费
        List<FeeValueVo>  feeValueVoList =  facilityInfoMapper.listFeeInfoByApartId(id);



        ApartmentDetailVo apartmentDetailVo = new ApartmentDetailVo();
        apartmentDetailVo.setGraphVoList(graphVoList);
        apartmentDetailVo.setLabelInfoList(labelInfoList);
        apartmentDetailVo.setFacilityInfoList(facilityInfoList);
        apartmentDetailVo.setFeeValueVoList(feeValueVoList);
        BeanUtils.copyProperties(apartmentInfo,apartmentDetailVo);


        return apartmentDetailVo;
    }

    @Override
    public void removeInfoById(Long id) {
        long count = roomInfoService.count(new LambdaQueryWrapper<RoomInfo>().eq(RoomInfo::getApartmentId,id));
        if (count==0){
        //删除公寓信息
        this.removeById(id);
        //1.删除GraphInfo
        LambdaQueryWrapper<GraphInfo> queryWrapper1 = new LambdaQueryWrapper<>();
        queryWrapper1.eq(GraphInfo::getItemType,ItemType.APARTMENT);
        queryWrapper1.eq(GraphInfo::getItemId,id);
        graphInfoService.remove(queryWrapper1);

        //2.删除ApartmentLabel
        LambdaQueryWrapper<ApartmentLabel> queryWrapper2 = new LambdaQueryWrapper<>();
        queryWrapper2.eq(ApartmentLabel::getLabelId,id);
        apartmentLabelService.remove(queryWrapper2);

        //3.删除ApartmentFacility
        LambdaQueryWrapper<ApartmentFacility> queryWrapper3 = new LambdaQueryWrapper<>();
        queryWrapper3.eq(ApartmentFacility::getFacilityId,id);
        apartmentFacilityService.remove(queryWrapper3);

        //4.删除ApartmentFeeValue
        LambdaQueryWrapper<ApartmentFeeValue> queryWrapper4 = new LambdaQueryWrapper<>();
        queryWrapper4.eq(ApartmentFeeValue::getFeeValueId,id);
        apartmentFeeValueService.remove(queryWrapper4);
    }else {
            throw new LeaseException(ResultCodeEnum.DELETE_ERROR);
        }

    }
}




